package cn.iocoder.yudao.module.crm.controller.admin.pricetemplate;

import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.crm.controller.admin.pricetemplate.vo.*;
import cn.iocoder.yudao.module.crm.dal.dataobject.pricetemplate.PriceTemplateDO;
import cn.iocoder.yudao.module.crm.service.pricetemplate.PriceTemplateService;

@Tag(name = "管理后台 - 船公司收费报价模版")
@RestController
@RequestMapping("/crm/price-template")
@Validated
public class PriceTemplateController {

    @Resource
    private PriceTemplateService priceTemplateService;

    @PostMapping("/create")
    @Operation(summary = "创建船公司收费报价模版")
    @PreAuthorize("@ss.hasPermission('crm:price-template:create')")
    public CommonResult<Long> createPriceTemplate(@Valid @RequestBody PriceTemplateSaveReqVO createReqVO) {
        return success(priceTemplateService.createPriceTemplate(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新船公司收费报价模版")
    @PreAuthorize("@ss.hasPermission('crm:price-template:update')")
    public CommonResult<Boolean> updatePriceTemplate(@Valid @RequestBody PriceTemplateSaveReqVO updateReqVO) {
        priceTemplateService.updatePriceTemplate(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除船公司收费报价模版")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('crm:price-template:delete')")
    public CommonResult<Boolean> deletePriceTemplate(@RequestParam("id") Long id) {
        priceTemplateService.deletePriceTemplate(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得船公司收费报价模版")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('crm:price-template:query')")
    public CommonResult<PriceTemplateRespVO> getPriceTemplate(@RequestParam("id") Long id) {
        PriceTemplateDO priceTemplate = priceTemplateService.getPriceTemplate(id);
        return success(BeanUtils.toBean(priceTemplate, PriceTemplateRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得船公司收费报价模版分页")
    @PreAuthorize("@ss.hasPermission('crm:price-template:query')")
    public CommonResult<PageResult<PriceTemplateRespVO>> getPriceTemplatePage(@Valid PriceTemplatePageReqVO pageReqVO) {
        PageResult<PriceTemplateDO> pageResult = priceTemplateService.getPriceTemplatePage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, PriceTemplateRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出船公司收费报价模版 Excel")
    @PreAuthorize("@ss.hasPermission('crm:price-template:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportPriceTemplateExcel(@Valid PriceTemplatePageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<PriceTemplateDO> list = priceTemplateService.getPriceTemplatePage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "船公司收费报价模版.xls", "数据", PriceTemplateRespVO.class,
                        BeanUtils.toBean(list, PriceTemplateRespVO.class));
    }

}